package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.feign.CouponFeign;
import com.shop.feign.SearchFeign;
import com.shop.mapper.Pms_Brand_Mapper;
import com.shop.mapper.Pms_Category_Mapper;
import com.shop.mapper.Pms_Spu_Info_Mapper;
import com.shop.pojo.pms.*;
import com.shop.service.*;
import com.shop.to.SkuByReductionTo;
import com.shop.to.SpuByBoundsTo;
import com.shop.utils.LayuiUtils;
import com.shop.utils.R;
import com.shop.vo.Attr;
import com.shop.vo.CategoryAndBrandBySpuIdVo;
import com.shop.vo.addspuvo.BaseAttrs;
import com.shop.vo.addspuvo.Images;
import com.shop.vo.addspuvo.Skus;
import com.shop.vo.addspuvo.SpuSaveVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class Pms_Spu_Info_ServiceImpl implements Pms_Spu_Info_Service {
    @Autowired
    private Pms_Spu_Info_Mapper pms_spu_info_mapper;
@Autowired
    private Pms_Spu_Desc_Service pms_spu_desc_service;
@Autowired
private Pms_Spu_Images_Service pms_spu_images_service;
@Autowired
private Pms_Attr_Service pms_attr_service;
@Autowired
private Pms_Product_Attr_Value_Service pms_product_attr_value_service;
@Autowired
private Pms_SkuInfo_Service skuInfo_service;
@Autowired
private Pms_Sku_Images_Service pms_sku_images_service;
@Autowired
private Pms_Sku_Sale_Value_Service sku_sale_value_service;
@Autowired
private CouponFeign couponFeign;
@Autowired
private Pms_Brand_Mapper pms_brand_mapper;
@Autowired
private Pms_Category_Mapper pms_category_mapper;
@Autowired
private SearchFeign searchFeign;


    @Transactional
    @Override
    public void saveSpuInfo(SpuSaveVo vo) {
          //1、保存spu基本信息  pms_spu_info
                Pms_Spu_Info pms_spu_info=new Pms_Spu_Info();
        pms_spu_info.setCreate_time(new Date());
        pms_spu_info.setUpdate_time(new Date());
        BeanUtils.copyProperties(vo,pms_spu_info);

        this.saveBaseSpuInfo(pms_spu_info);
        System.out.println("我是主键"+pms_spu_info.getId());

        //2、保存Spu的描述图片 pms_sku_info_desc
        Pms_Spu_Info_Desc pms_spu_info_desc=new Pms_Spu_Info_Desc();
        pms_spu_info_desc.setSpu_id(pms_spu_info.getId());
        pms_spu_info_desc.setDecript(String.join(",",vo.getDecript()));
        pms_spu_desc_service.saveSpuInfoDesc(pms_spu_info_desc);
        //3、保存spu的图片集 pms_spu_images
        List<String> images = vo.getImages();//还未进行任何操作
        pms_spu_images_service.saveImages(pms_spu_info.getId(),images);
        //4、保存spu的规格参数 pms_product_attr_value
        List<BaseAttrs> baseAttrs = vo.getBaseAttrs();
        List<Pms_Product_Attr_Value> collect = baseAttrs.stream().map((x) -> {
            Pms_Product_Attr_Value pms_product_attr_value = new Pms_Product_Attr_Value();
            pms_product_attr_value.setAttr_id(x.getAttrId());
            Pms_Attr pms_attr = pms_attr_service.PMS_ATTRById(x.getAttrId());
            pms_product_attr_value.setAttr_name(pms_attr.getAttr_name());
            pms_product_attr_value.setAttr_value(x.getAttrValues());
            pms_product_attr_value.setQuick_show(x.getShowDesc());
            pms_product_attr_value.setSpu_id(pms_spu_info.getId());
            return pms_product_attr_value;
        }).collect(Collectors.toList());
        pms_product_attr_value_service.saveProductAttr(collect);
        //5、保存spu的积分信息 sms_spu_bounds
        SpuByBoundsTo spuByBoundsTo=new SpuByBoundsTo();
          spuByBoundsTo.setSpu_id(pms_spu_info.getId());
          spuByBoundsTo.setBuy_bounds(vo.getBounds().getBuyBounds());
          spuByBoundsTo.setGrow_bounds(vo.getBounds().getGrowBounds());
        couponFeign.saveBoundsBySpu(spuByBoundsTo);//调用远程设置spu积分信息

        //6、保存当前spu对应的所有sku信息
        //6.1)sku的基本信息
        List<Skus> skus = vo.getSkus();
         if(skus!=null&&skus.size()>0){
             skus.forEach(item ->{
                 Pms_Sku_Info pms_sku_info=new Pms_Sku_Info();
                   BeanUtils.copyProperties(item,pms_sku_info);
                 pms_sku_info.setBrand_id(pms_spu_info.getBrand_id());
                 pms_sku_info.setCatalog_id(pms_spu_info.getCatalog_id());
                 pms_sku_info.setSale_count(0L);
                 pms_sku_info.setSpu_id(pms_spu_info.getId());
                 skuInfo_service.saveSkuInfo(pms_sku_info);
                 Long sku_id = pms_sku_info.getSku_id();
                 List<Pms_Sku_Images> imagesList = item.getImages().stream().map((x) -> {
                     Pms_Sku_Images sku_images = new Pms_Sku_Images();
                     sku_images.setSku_id(sku_id);
                     sku_images.setImg_url(x);
                     sku_images.setDefault_img(0);
                     return sku_images;
                 }).collect(Collectors.toList());
                 //6.2)sku的图片信息
                 pms_sku_images_service.saveBatch(imagesList);
                 //6.3)sku的销售属性信息
                 List<Attr> attr = item.getAttr();
                 List<Pms_Sku_Sale_Attr_Value> pms_sku_sale_attr_values = attr.stream().map((xattr) -> {
                     Pms_Sku_Sale_Attr_Value sku_sale_attr_value = new Pms_Sku_Sale_Attr_Value();
                     BeanUtils.copyProperties(xattr, sku_sale_attr_value);
                     sku_sale_attr_value.setSku_id(sku_id);
                     return sku_sale_attr_value;
                 }).collect(Collectors.toList());
                 sku_sale_value_service.saveBatch(pms_sku_sale_attr_values);
                 //6.4)sku的优惠、满减等信息 sms_sku_ladder/sms_sku_full_reduction/sms_merber_price
                 SkuByReductionTo skuByReductionTo=new SkuByReductionTo();
                 BeanUtils.copyProperties(item,skuByReductionTo);
                 skuByReductionTo.setSku_id(sku_id);
                 R r = couponFeign.addReduction(skuByReductionTo);
                 System.out.println(r);

             });

         }



    }



    @Override
    public void saveBaseSpuInfo(Pms_Spu_Info pms_spu_info) {
        //添加spu信息
        pms_spu_info_mapper.insert(pms_spu_info);
    }
    /*
     * @Description 多条件查询全部spu信息
     * @param pms_spu_info
     * 根据分类
     * 根据品牌
     * 根据上下架状态
     * 根据spu名称
     * @return com.shop.utils.LayuiUtils<com.shop.pojo.pms.Pms_Spu_Info>
     * @author liusupeng
     * @date 2020/11/8 18:50
     */
    @Override
    public LayuiUtils<Pms_Spu_Info> PMS_SPU_INFO_LAYUI_UTILS(Pms_Spu_Info pms_spu_info,Integer page) {

        LayuiUtils<Pms_Spu_Info> pms_spu_infoLayuiUtils=new LayuiUtils<>();
        QueryWrapper<Pms_Spu_Info> pms_spu_infoQueryWrapper=new QueryWrapper<>();
        if(pms_spu_info.getCatalog_id()!=null&&pms_spu_info.getCatalog_id()>0){
            pms_spu_infoQueryWrapper.eq("catalog_id",pms_spu_info.getCatalog_id());
        }
        if(pms_spu_info.getBrand_id()!=null&&pms_spu_info.getBrand_id()>0){
            pms_spu_infoQueryWrapper.eq("brand_id",pms_spu_info.getBrand_id());
        }
        if(pms_spu_info.getPublish_status()!=null&&pms_spu_info.getPublish_status()>0){
            pms_spu_infoQueryWrapper.eq("publish_status",pms_spu_info.getPublish_status());
        }
        if(pms_spu_info.getSpu_name()!=null&&pms_spu_info.getSpu_name()!=""){
            pms_spu_infoQueryWrapper.like("spu_name",pms_spu_info.getSpu_name());
        }
        Page<Pms_Spu_Info> pms_spu_infoPage=new Page<>(page,10);
        Page<Pms_Spu_Info> spu_infoPage = pms_spu_info_mapper.selectPage(pms_spu_infoPage, pms_spu_infoQueryWrapper);
        pms_spu_infoLayuiUtils.setData(spu_infoPage.getRecords());
        pms_spu_infoLayuiUtils.setCount(spu_infoPage.getTotal());
        return pms_spu_infoLayuiUtils;
    }
    /*
     * @Description 修改spu的上下架。
     * 后期需要修改es，现在测试暂无es部分
     * @param pms_spu_info
     * @return com.shop.utils.R
     * @author liusupeng
     * @date 2020/11/8 19:23
     */
    @Override
    public R exitSpuByStatus(Pms_Spu_Info pms_spu_info) {
        if(pms_spu_info.getPublish_status()==0){
            //下架
            R r = searchFeign.delSku(pms_spu_info.getId());
            if(r.getCode()!=0){
                return r;
            }
        }else{
            //上架
            R r = searchFeign.addSku(pms_spu_info.getId());
            if(r.getCode()!=0){
                return r;
            }
        }
        int i = pms_spu_info_mapper.updateById(pms_spu_info);
        if(i>0){
            return R.success();
        }
        return R.error(500,"服务器响应中");
    }
    /*
     * @Description 遍历spu，拿到所有spu下的分类id和品牌id并添加到list集合里
     * 将这两个集合里的id分别去品牌表和分类表里进行查询
     * 最终将查询到的list集合赋值给新建的vo类
     *
     * @param
     * @return com.shop.utils.R
     * @author liusupeng
     * @date 2020/11/8 19:49
     */
    @Override
    public R CATEGORY_AND_BRAND_BY_SPU_ID() {
        List<Pms_Spu_Info> pms_spu_infos = pms_spu_info_mapper.selectList(new QueryWrapper<>());
      List<Long> catalogId=new ArrayList<>();//全部分类id
        List<Long> brandId=new ArrayList<>();//全部品牌id
        pms_spu_infos.stream().forEach(x->{
            catalogId.add(x.getCatalog_id());
            brandId.add(x.getBrand_id());
        });
        QueryWrapper<Pms_Brand> pms_brandQueryWrapper=new QueryWrapper<>();
        pms_brandQueryWrapper.in("brand_id",brandId);
        List<Pms_Brand> pms_brands = pms_brand_mapper.selectList(pms_brandQueryWrapper);
        QueryWrapper<Pms_Category> pms_categoryQueryWrapper=new QueryWrapper<>();
        pms_categoryQueryWrapper.in("cat_id",catalogId);
        List<Pms_Category> pms_categories = pms_category_mapper.selectList(pms_categoryQueryWrapper);
        CategoryAndBrandBySpuIdVo categoryAndBrandBySpuIdVo=new CategoryAndBrandBySpuIdVo();
        categoryAndBrandBySpuIdVo.setPms_brandList(pms_brands);
        categoryAndBrandBySpuIdVo.setPms_categoryList(pms_categories);
        R r1=new R();
        r1.data(categoryAndBrandBySpuIdVo);
        return r1;
    }
}
